package com.fingard.dsp.bank.directbank.jrtt02;

import com.bytedance.ea.bankconnector.api.AccountQueryService;
import com.bytedance.ea.bankconnector.api.dto.AccountBalanceDto;
import com.bytedance.ea.bankconnector.api.dto.AccountQueryReq;
import com.bytedance.ea.bankconnector.api.dto.AccountQueryRsp;
import com.bytedance.ea.bankconnector.api.dto.BankBaseReq;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.util.DateHelper;

import java.util.Calendar;
import java.util.Date;

/**
 * 财经-海外接口
 */
public class JRTT02Balance extends JRTT02Base implements IBalance{

    /**
     * 查询余额
     * @param p_req7506
     * @return
     * @throws Exception
     */
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception{
        RetAts7506BalQry retAts7506BalQry = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts7506BalQry.writeStart();
        try{
            RetAtsBalDetail detail = new RetAtsBalDetail();
            while (p_req7506.readRecord()) {
                //BaseReq
                BankBaseReq ruleBaseReq=writeBankBaseReq(new JRTT02Header());
                //AccountQueryReq
                AccountQueryReq accountQueryReq=new AccountQueryReq();
                accountQueryReq.setQueryType("BALI");//实时余额
                accountQueryReq.setBankType(p_req7506.readedDetail.bankType);//银行体系
                accountQueryReq.setPayAccount(p_req7506.readedDetail.actNum);//付款帐号
                accountQueryReq.setPayIban(p_req7506.readedDetail.payIBAN);//付款IBAN账号
                //查询时间日期
                Calendar tmpCalendarFrom = Calendar.getInstance();
                tmpCalendarFrom.setTime(Format.DateTimeFormat.parse(Format.DateTimeFormat.format(new Date())));
                accountQueryReq.setFrDt(getGlobalLocalDate(tmpCalendarFrom.getTime(),p_req7506.readedDetail.timeZone));//该日期为账户时区的当日日期
                accountQueryReq.setToDt(Format.Date10Format.format(new Date()));//该日期为输入日期
                accountQueryReq.setTimeZone(p_req7506.readedDetail.timeZone);//时差
                accountQueryReq.setBaseReq(ruleBaseReq);
                //Client
                DubboBeanFactory beanFactory = new DubboBeanFactory(bankFront,getBankConfig());
                AccountQueryService.Iface accountClient=beanFactory.getAccountService();
                WriteBankLogStep2("AccountQueryReq："+accountQueryReq);
                AccountQueryRsp accountQueryRsp=accountClient.execute(accountQueryReq);
                WriteBankLogStep3("AccountQueryRsp："+accountQueryRsp);
                //处理响应报文
                if (accountQueryRsp.getBaseRsp() != null) {
                    String returnCode = accountQueryRsp.getBaseRsp().getResponseStatus();//返回码
                    String returnMsg = accountQueryRsp.getBaseRsp().getAddtlInf();//返回信息
                    if("ACCP".equals(returnCode)){
                        detail.actNum = p_req7506.readedDetail.actNum;//账号
                        if(accountQueryRsp.getBalanceList()!=null && accountQueryRsp.getBalanceList().size()>0){
                            for(AccountBalanceDto accountBalanceDto:accountQueryRsp.getBalanceList()){
                                detail.respCode = "0";//响应码
                                detail.respInfo = returnMsg;//响应信息
                                detail.actCur = convertCurToATS(accountBalanceDto.getCurrency());//币种
                                detail.availBal = amountChange(accountBalanceDto.getBalance());//可用余额
                                detail.currBal = amountChange(accountBalanceDto.getTotalBalance());//余额
                            }
                        }
                    }else{
                        detail.respCode = "-1";
                        detail.respInfo = returnMsg;
                    }
                }else{
                    detail.respCode = "-1";
                    detail.respInfo = "响应BaseRsp为空";
                }
                retAts7506BalQry.writeRetDetail(detail, p_req7506.isBatch);
            }
        }catch (Exception e){
            WriteBankLogLn("查询余额异常"+e);
            retAts7506BalQry.writeRespCode("-1");
            retAts7506BalQry.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts7506BalQry.writeEnd();
        return retAts7506BalQry;
    }


    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception{
        //增加历史明细支持查询日期
        if(StringHelper.hasAnyChar(getBankConfig().getValueAsString("hisTransSupDate"))
                && getBankConfig().getValueAsString("hisTransSupDate").length()==8){
            //判断起始日
            Date avaiDate = Format.Date8Format.parse(getBankConfig().getValueAsString("hisTransSupDate"));
            Date startDate = Format.Date8Format.parse(p_req7505.dateFrom);
            Date endDate = Format.Date8Format.parse(p_req7505.dateTo);
            if(endDate.after(avaiDate)){
                if(startDate.before(avaiDate)){
                    p_req7505.dateFrom=getBankConfig().getValueAsString("hisTransSupDate");
                }
            }else{
                p_req7505.dateFrom=getBankConfig().getValueAsString("hisTransSupDate");
                p_req7505.dateTo=getBankConfig().getValueAsString("hisTransSupDate");
            }
        }
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try{
            //BaseReq
            BankBaseReq ruleBaseReq=writeBankBaseReq(new JRTT02Header());
            //AccountQueryReq
            AccountQueryReq accountQueryReq=new AccountQueryReq();
            accountQueryReq.setQueryType("BALH");//历史余额
            accountQueryReq.setBankType(p_req7505.bankType);//银行体系
            accountQueryReq.setPayAccount(p_req7505.actNum);//付款帐号
            accountQueryReq.setPayIban(p_req7505.payIBAN);//付款IBAN账号
            accountQueryReq.setFrDt(DateHelper.convertD8toD10(p_req7505.dateFrom));//该日期为输入日期
            accountQueryReq.setToDt( DateHelper.convertD8toD10(p_req7505.dateTo));//该日期为输入日期
            accountQueryReq.setTimeZone(p_req7505.timeZone);//时差
            accountQueryReq.setBaseReq(ruleBaseReq);
            //Client
            DubboBeanFactory beanFactory = new DubboBeanFactory(bankFront,getBankConfig());
            AccountQueryService.Iface accountClient=beanFactory.getAccountService();
            WriteBankLogStep2("AccountQueryReq："+accountQueryReq);
            AccountQueryRsp accountQueryRsp=accountClient.execute(accountQueryReq);
            WriteBankLogStep3("AccountQueryRsp："+accountQueryRsp);
            //处理响应报文
            if (accountQueryRsp.getBaseRsp() != null) {
                String returnCode = accountQueryRsp.getBaseRsp().getResponseStatus();//返回码
                String returnMsg = accountQueryRsp.getBaseRsp().getAddtlInf();//返回信息
                if("ACCP".equals(returnCode)){
                    if(accountQueryRsp.getBalanceList()!=null && accountQueryRsp.getBalanceList().size()>0){
                        for(AccountBalanceDto accountBalanceDto:accountQueryRsp.getBalanceList()){
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            tmpRetDetail.actBal = amountChange(accountBalanceDto.getBalance());//可用余额
                            tmpRetDetail.balDate = DateHelper.convertD8toD10(accountBalanceDto.getAcDate());//余额时间
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    }else {
                        tmpRespCode = "-1";
                        tmpRespInfo = returnMsg;
                    }
                }else{
                    tmpRespCode = "-1";
                    tmpRespInfo = returnMsg;
                }
            }else{
                tmpRespCode = "-1";
                tmpRespInfo = "响应BaseRsp为空";
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
